package com.xxp.controller;

import com.xxp.common.Result;
import com.xxp.controller.request.UserPageRequest;
import com.xxp.entity.User;
import com.xxp.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin    //跨域解决方法
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @PostMapping("/add")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }
    @GetMapping("/list")//查询所有
    public Result list(){
        List<User> users = userService.list();
        return Result.success(users);
    }
    @GetMapping("/page")//分页模糊查询
    public Result page(UserPageRequest userPageRequest){
        return Result.success(userService.page(userPageRequest));
    }
    @GetMapping("/{id}")//根据Id查询
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }
    @PutMapping("/update")//根据ID修改
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        userService.deleteById(id);
        return Result.success();
    }
}
